package com.ijiuchuang.jc_localization.view.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.AppCompatSpinner;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ijiuchuang.jc_localization.R;
import com.ijiuchuang.jc_localization.bean.lite.Setts;
import com.ijiuchuang.jc_localization.contract.settings.SettingsContract;
import com.ijiuchuang.jc_localization.presenter.settings.SettingsPresenter;
import com.ijiuchuang.jc_localization.tools.utils.Log;
import com.ijiuchuang.jc_localization.tools.utils.MyUtil;
import com.ijiuchuang.jc_localization.view.activity.FragmentActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnItemSelected;

/**
 * A simple {@link Fragment} subclass.
 */
public class SettingsFragment extends Fragment implements SettingsContract.View {

    @BindView(R.id.settings_linear_text_version_name)
    TextView textVersionName;
    @BindView(R.id.settings_linear_check_version)
    LinearLayout linearCheckVersion;
    @BindView(R.id.settings_linear_open_source)
    LinearLayout linearOpenSource;
    @BindView(R.id.settings_linear_banner_spinner)
    AppCompatSpinner spinnerBanner;

    private SettingsContract.Presenter presenter;

    public SettingsFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_settings, container, false);
        ButterKnife.bind(this, view);

        presenter = new SettingsPresenter(getActivity(), this);
        presenter.start();

        textVersionName.append(MyUtil.getVersion(getActivity()));


        return view;
    }

    @OnClick({R.id.settings_linear_banner,
            R.id.settings_linear_location,
            R.id.settings_linear_open_source,
            R.id.settings_linear_text_version_name,
            R.id.settings_linear_about,
            R.id.settings_linear_check_version})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.settings_linear_banner:
                break;
            case R.id.settings_linear_location:
                Intent intentLocation = new Intent(getActivity(), FragmentActivity.class);
                intentLocation.putExtra(FragmentActivity.FRAGMENT_NAME, FragmentActivity.FRAGMENT_NAME_SETTS_LOCATION);
                startActivity(intentLocation);
                break;
            case R.id.settings_linear_open_source:
                Intent intentOpen = new Intent(getActivity(), FragmentActivity.class);
                intentOpen.putExtra(FragmentActivity.FRAGMENT_NAME, FragmentActivity.FRAGMENT_NAME_OPEN_SOURCE);
                startActivity(intentOpen);
                break;
            case R.id.settings_linear_about:
                Intent intentAbout = new Intent(getActivity(), FragmentActivity.class);
                intentAbout.putExtra(FragmentActivity.FRAGMENT_NAME, FragmentActivity.FRAGMENT_NAME_ABOUT);
                startActivity(intentAbout);
                break;
            case R.id.settings_linear_check_version:
                presenter.checkVersion();
                break;
        }
    }

    @Override
    public void setPresenter(SettingsContract.Presenter presenter) {
        this.presenter = presenter;
    }

    @OnItemSelected(R.id.settings_linear_banner_spinner)
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        presenter.save(Setts.SETTINGS_HOME_BANNER_MODE, position);
        Log.i("position: " + position);
    }

    @Override
    public void setBannerMode(int mode) {
        spinnerBanner.setSelection(mode);
    }
}
